package com.xqboss.apps.listener;

import com.xqboss.apps.enums.mq.AppMqNormalEnum;
import com.xqboss.apps.service.order.OrderService;
import com.xqboss.common.core.mq.MqEnum;
import com.xqboss.common.core.mq.MqListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.apis.consumer.ConsumeResult;
import org.apache.rocketmq.client.apis.message.MessageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 市场订单结算任务
 */
@Slf4j
@Component
public class OrderSettleListener extends MqListener {

    @Autowired
    private OrderService orderService;

    @Override
    public MqEnum getEnum() {
        return AppMqNormalEnum.ORDER_SETTLE;
    }

    @Override
    public ConsumeResult solve(MessageView message) {
        String value = StandardCharsets.UTF_8.decode(message.getBody()).toString();
        long orderId = Long.parseLong(value);
        try{
            orderService.settleMarketOrder(orderId);
        }catch (Exception e){
            log.error("订单结算失败", e);
            return ConsumeResult.FAILURE;
        }
        return ConsumeResult.SUCCESS;
    }

}
